<?php

declare(strict_types=1);
/**
 * This file is part of MoChat.
 * @link     https://mo.chat
 * @document https://mochat.wiki
 * @contact  group@mo.chat
 * @license  https://github.com/mochat-cloud/mochat/blob/master/LICENSE
 */
namespace MoChat\App\WorkContact\Action\Dashboard;

use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\RequestMapping;
use MoChat\App\Common\Middleware\DashboardAuthMiddleware;
use MoChat\App\Rbac\Middleware\PermissionMiddleware;
use MoChat\App\WorkContact\Contract\WorkContactContract;
use MoChat\App\WorkEmployee\Contract\WorkEmployeeContract;
use MoChat\Framework\Action\AbstractAction;
use MoChat\Framework\Constants\ErrorCode;
use MoChat\Framework\Exception\CommonException;
use MoChat\Framework\Request\ValidateSceneTrait;
use MoChat\Plugin\AutoTag\Action\Dashboard\Traits\AutoContactTag;

/**
 * 员工编辑客户标签.
 *
 * Class EmployeeCreateContactTag
 * @Controller
 */
class EmployeeCreateContactTag extends AbstractAction
{
    use ValidateSceneTrait;
    use AutoContactTag;

    /**
     * @Inject
     * @var WorkEmployeeContract
     */
    protected $workEmployeeContract;

    /**
     * @Inject
     * @var WorkContactContract
     */
    private $workContactContract;

    /**
     * @Middlewares({
     *     @Middleware(DashboardAuthMiddleware::class),
     *     @Middleware(PermissionMiddleware::class)
     * })
     * @RequestMapping(path="/dashboard/workContact/employeeCreateContactTag", methods="PUT")
     * @return array
     */
    public function handle()
    {
        // 接收参数
        $params['contactId'] = (int) $this->request->input('contactId');
        $params['employeeId'] = (int) $this->request->input('employeeId');
        $params['tagArr'] = $this->request->input('tagIds');

        // 校验参数
        $this->validated($params);
        // 客户
        $contact = $this->workContactContract->getWorkContactById($params['contactId'], ['wx_external_userid']);
        if (empty($contact)) {
            throw new CommonException(ErrorCode::INVALID_PARAMS, '客户已删除，打标签失败');
        }
        $params['contactWxExternalUserid'] = $contact['wxExternalUserid'];
        // 员工
        $employee = $this->workEmployeeContract->getWorkEmployeeById(user()['id'], ['wx_user_id']);
        if (empty($employee)) {
            throw new CommonException(ErrorCode::INVALID_PARAMS, '员工已离职，打标签失败');
        }
        $params['employeeWxUserId'] = $employee['wxUserId'];
        $this->autoTag($params);
        $this->deleteTag($params);
        return [];
    }

    /**
     * @return string[] 规则
     */
    public function rules(): array
    {
        return [
            'contactId' => 'required|integer|min:1|bail',
            'employeeId' => 'required|integer|min:1|bail',
            'tagArr' => 'required|array',
        ];
    }

    /**
     * 获取已定义验证规则的错误消息.
     */
    public function messages(): array
    {
        return [
            'contactId.required' => '客户id必传',
            'employeeId.required' => '员工id必传',
            'tagArr.required' => '标签id必传',
        ];
    }
}
